package com.zeng.sboot.rt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class RuntimeDemo {

    private static final Logger logger = LoggerFactory.getLogger(RuntimeDemo.class);

    public void test1() throws IOException {
        // 直接打开应用程序
        Runtime.getRuntime().exec("C:/Users/liqiang/Desktop/开机后点它.bat"); // 打开一个批处理文件
        Runtime.getRuntime().exec("E:/酷狗/KGMusic/KuGou.exe"); // 打开酷狗

        /******** 可以通过cmd命令打开软件或者是做其他 *****/
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /k start E:/酷狗/KGMusic/KuGou.exe"); // 通过cmd窗口执行命令
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /k start E:/php/Test/第一个html/界面.html"); // 通过cmd命令打开一个网页
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /k mkdir C:\\Users\\liqiang\\Desktop\\java键的1"); // 通过cmd创建目录用两个反斜杠
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /k mkdir C:\\Users\\liqiang\\Desktop\\java键的2"); // 通过cmd创建目录用两个反斜杠
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /c calc ");// 通过cmd打开计算器
    }

    public void test2() throws IOException {
        /******** 可以通过cmd命令打开软件或者是做其他 *****/
        Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /c osk");// 通过屏幕软键盘
    }

    //==================== 上面的方式都是异步运行的方式，也就是在执行命令之后会不等exec执行完就执行下一条语句

    //为了实现同步结果，或者为了获取返回的结果
    public void test3() {
        long start = System.currentTimeMillis();
        String srcPath = "C:\\Users\\zengzhijun\\Desktop\\ww\\aa.docx", desPath = "C:/Users/zengzhijun/Desktop/ww";
        String command = "";
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            command = "soffice --headless --convert-to pdf " + srcPath + " --outdir " + desPath;
            exec(command);
        }
        long end = System.currentTimeMillis();
        logger.debug("用时:{} ms", end - start);
    }

    public static boolean exec(String command) {
        Process process;// Process可以控制该子进程的执行或获取该子进程的信息
        try {
            logger.debug("exec cmd : {}", command);
            process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序，并返回与该子进程对应的Process对象实例。
            // 下面两个可以获取输入输出流
            InputStream errorStream = process.getErrorStream();
            InputStream inputStream = process.getInputStream();
        } catch (IOException e) {
            logger.error(" exec {} error", command, e);
            return false;
        }

        int exitStatus = 0;
        try {
            exitStatus = process.waitFor();// 等待子进程完成再往下执行，返回值是子线程执行完毕的返回值
            // 第二种接受返回值的方法
            int i = process.exitValue(); // 接收执行完毕的返回值
            logger.debug("i----" + i);
        } catch (InterruptedException e) {
            logger.error("InterruptedException  exec {}", command, e);
            return false;
        }

        if (exitStatus != 0) {
            logger.error("exec cmd exitStatus {}", exitStatus);
        } else {
            logger.debug("exec cmd exitStatus {}", exitStatus);
        }

        process.destroy(); // 销毁子进程
        process = null;

        return true;
    }

    public void test4() {
        try {
            Process pop = Runtime.getRuntime()
                    .exec("E:/weblogic12.1.3/user_projects/domains/base_domain/startWebLogic.cmd");
            // 获取其正常的输出流
            InputStream inputStream = pop.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(inputStreamReader);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

            // 获取其错误的输出流
            InputStream errorStream = pop.getErrorStream();
            InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
            BufferedReader errorBr = new BufferedReader(errorStreamReader);
            String errorLine = null;
            while ((errorLine = errorBr.readLine()) != null) {
                System.out.println("err:" + errorLine);
            }

            pop.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void test5() {
        long totalMemory = Runtime.getRuntime().totalMemory();//总内存
        long freeMemory = Runtime.getRuntime().freeMemory();//剩余内存
        long maxMemory = Runtime.getRuntime().maxMemory();//最大内存
        System.out.println(totalMemory/1024/1024+"MB");
        System.out.println(freeMemory/1024/1024+"MB");
        System.out.println(maxMemory/1024/1024+"MB");

        try {
            Runtime.getRuntime().exec("calc");//打开计算器
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Runtime.getRuntime().availableProcessors());

    }


    public static void main(String[] args) {
        RuntimeDemo demo = new RuntimeDemo();

        demo.test5();
    }
}
